package cn.edu.pet.servlet;

import cn.edu.pet.entity.User;
import cn.edu.pet.repo.UserRepo;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        ObjectMapper mapper = new ObjectMapper();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String tel = req.getParameter("tel");
        String gender = req.getParameter("gender");
        String address = req.getParameter("address");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setTel(tel);
        user.setGender(gender);
        user.setAddress(address);
        UserRepo.getInstance().insertUser(user);
        PrintWriter writer = resp.getWriter();
        String json = mapper.writeValueAsString("注册成功!");
        writer.write(json);
    }
}
